找传奇、传世资源到传世资源站!

安卓日期选择器_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

安卓日期选择器_ Android平台开发-第1张

package com.byl.datepicker;


import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import com.byl.datepicker.wheelview.OnWheelScrollListener;
import com.byl.datepicker.wheelview.WheelView;
import com.byl.datepicker.wheelview.adapter.ArrayWheelAdapter;
import com.byl.datepicker.wheelview.adapter.NumericWheelAdapter;
import com.byl.datepicker.wheelview.adapter.WeekWheelAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private LayoutInflater inflater = null;
	private WheelView year;
	private WheelView month;
	private WheelView day;
	private WheelView time;
	private WheelView min;
	private WheelView sec;
	private WheelView week;
	
	private int mYear=1996;
	private int mMonth=0;
	private int mDay=1;
	
	LinearLayout ll;
	TextView tv1,tv2;
	
	 View view=null;
	
	boolean isMonthSetted=false,isDaySetted=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		ll=(LinearLayout) findViewById(R.id.ll);
		ll.addView(getDataPick());
		tv1=(TextView) findViewById(R.id.tv1);//年龄
		tv2=(TextView) findViewById(R.id.tv2);//星座
	}
	
	private View getDataPick() {
		Calendar c = Calendar.getInstance();
		int norYear = c.get(Calendar.YEAR);
//		int curMonth = c.get(Calendar.MONTH)  1;//通过Calendar算出的月数要 1
//		int curDate = c.get(Calendar.DATE);
		
		int curYear = mYear;
		int curMonth =mMonth 1;
		int curDate = mDay;
		
		view = inflater.inflate(R.layout.wheel_date_picker, null);
		
		year = (WheelView) view.findViewById(R.id.year);
		NumericWheelAdapter numericWheelAdapter1=new NumericWheelAdapter(this,1950, norYear); 
		numericWheelAdapter1.setLabel("年");
		year.setViewAdapter(numericWheelAdapter1);
		year.setCyclic(true);//是否可循环滑动
		year.addScrollingListener(scrollListener);
		
		month = (WheelView) view.findViewById(R.id.month);
		NumericWheelAdapter numericWheelAdapter2=new NumericWheelAdapter(this,1, 12, "%02d"); 
		numericWheelAdapter2.setLabel("月");
		month.setViewAdapter(numericWheelAdapter2);
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);
		
		day = (WheelView) view.findViewById(R.id.day);
		initDay(curYear,curMonth);
		day.setCyclic(true);

		
		min = (WheelView) view.findViewById(R.id.min);
		NumericWheelAdapter numericWheelAdapter3=new NumericWheelAdapter(this,1, 23, "%02d"); 
		numericWheelAdapter3.setLabel("时");
		min.setViewAdapter(numericWheelAdapter3);
		min.setCyclic(true);
		min.addScrollingListener(scrollListener);
		
		sec = (WheelView) view.findViewById(R.id.sec);
		NumericWheelAdapter numericWheelAdapter4=new NumericWheelAdapter(this,1, 59, "%02d"); 
		numericWheelAdapter4.setLabel("分");
		sec.setViewAdapter(numericWheelAdapter4);
		sec.setCyclic(true);
		sec.addScrollingListener(scrollListener);
		
		
		week= (WheelView) view.findViewById(R.id.week);
		WeekWheelAdapter weekWheelAdapter5=new WeekWheelAdapter(this,1, 6, "%02d"); 
		weekWheelAdapter5.setLabel("星期");
		week.setViewAdapter(weekWheelAdapter5);
		week.setCyclic(true);
		week.addScrollingListener(scrollListener);
		
		year.setVisibleItems(7);//设置显示行数
		month.setVisibleItems(7);
		day.setVisibleItems(7);
		min.setVisibleItems(7);
		sec.setVisibleItems(7);
		week.setVisibleItems(7);
		
		year.setCurrentItem(curYear - 1950);
		month.setCurrentItem(curMonth - 1);
		day.setCurrentItem(curDate - 1);
		
		return view;
	}
	
	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
		@Override
		public void onScrollingStarted(WheelView wheel) {

		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			int n_year = year.getCurrentItem()  1950;//年
			int n_month = month.getCurrentItem()  1;//月
			
			initDay(n_year,n_month);
			
			String birthday=new StringBuilder().append((year.getCurrentItem() 1950)).append("-").append((month.getCurrentItem()  1) < 10 ? "0"  (month.getCurrentItem()  1) : (month.getCurrentItem()  1)).append("-").append(((day.getCurrentItem() 1) < 10) ? "0"  (day.getCurrentItem() 1) : (day.getCurrentItem() 1)).toString();
			tv1.setText("年龄       " calculateDatePoor(birthday) "岁");
			tv2.setText("星座       " getAstro(month.getCurrentItem()  1,day.getCurrentItem() 1));
			
			Toast.makeText(MainActivity.this, week.getCurrentItem() "",
					Toast.LENGTH_LONG).show();
		}
	};

	/**
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private int getDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = flag ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}

	/**
	 */
	private void initDay(int arg1, int arg2) {
		NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(this,1, getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setLabel("日");
		day.setViewAdapter(numericWheelAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	/**
	 * 根据日期计算年龄
	 * @param birthday
	 * @return
	 */
	public static final String calculateDatePoor(String birthday) {
		try {
			if (TextUtils.isEmpty(birthday))
				return "0";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date birthdayDate = sdf.parse(birthday);
			String currTimeStr = sdf.format(new Date());
			Date currDate = sdf.parse(currTimeStr);
			if (birthdayDate.getTime() > currDate.getTime()) {
				return "0";
			}
			long age = (currDate.getTime() - birthdayDate.getTime())
					/ (24 * 60 * 60 * 1000)  1;
			String year = new DecimalFormat("0.00").format(age / 365f);
			if (TextUtils.isEmpty(year))
				return "0";
			return String.valueOf(new Double(year).intValue());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "0";
	}
	
	/**
	 * 根据月日计算星座
	 * @param month
	 * @param day
	 * @return
	 */
	public String getAstro(int month, int day) {
     String[] astro = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座",
             "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座" };
     int[] arr = new int[] { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };// 两个星座分割日
     int index = month;
     // 所查询日期在分割日之前,索引-1,否则不变
     if (day < arr[month - 1]) {
         index = index - 1;
     }
     // 返回索引指向的星座string
     return astro[index];
 }

}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复